﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
    public class UsersDAO
    {
        CameraDBEntities db = new CameraDBEntities();
        public User DangNhap(string acc, string pass)
        {
            return db.Users.SingleOrDefault(x=>x.userAccount == acc && x.userPass== pass);
        }
        public User KiemTraUserName(string acc)
        {
            return db.Users.SingleOrDefault(x => x.userAccount == acc);
        }
        public User GetByUserId(int id)
        {
            return db.Users.SingleOrDefault(x => x.userId ==id);
        }
        public bool Insert(User c)
        {
            try
            {
                db.Users.Add(c);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Edit(User ca)
        {
            try
            {
                User a = new User();
                a = db.Users.Where(x => x.userId == ca.userId).SingleOrDefault();
                a.userAccount = ca.userAccount;
                a.userPass = ca.userPass;
                a.userName = ca.userName;
                a.userEmail = ca.userEmail;
                a.userPhone = ca.userPhone;
                a.userAddress = ca.userAddress;
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                User a = new User();
                a = db.Users.Where(x => x.userId == id).SingleOrDefault();
                db.Users.Remove(a);

                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        } 
    }
}
